define(function(require, exports){

	var utilDSP = require('modules/common');

	// 投放管理主页
	exports.onMain = function( boot, data, app ) {
		if(utilDSP.isBrandVersion()){
			// app.controller.navigate('');
		}
		if(!app.routerValid('')) {
			return;
		}
		var title = LANG('投放管理');
		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title:title,
			type: 'tabSidebar',
			content_config: {
				'addClass': 'cool'
			}
		});

		app.core.get('SYS_PLATFORM').setPlatform(title);


		con.createBusiness(data.name + 'advertiser', 'advertiser/action/list.main', {
			tabText: LANG('广告主'),
			tabType: 'advertiser',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'plan', 'plan/action/list.base', {
			tabText: LANG('计划'),
			tabType: 'plan',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'campaign', 'campaign/action/list.main', {
			tabText: LANG('推广组'),
			tabType: 'campaign',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'sweety', 'sweety/action/list.base', {
			tabText: LANG('创意包'),
			tabType: 'sweety',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'whisky', 'whisky/action/list.base', {
			tabText: LANG('落地页'),
			tabType: 'whisky',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});
		//广告监测
		con.createBusiness(data.name + 'monitor', 'monitor/action/list.list', {
			tabText: LANG('广告监测'),
			tabType: 'monitor',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});
	};

	// 广告主详情
	exports.onAdvertiser = function( boot, data, app ) {
		if(utilDSP.isBrandVersion()){
			// app.controller.navigate('');
		}
		if(!app.routerValid('')) {
			return;
		}
		var title = LANG('广告主详情');
		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title:title,
			type: 'tabSidebar',
			content_config: {
				'addClass': 'cool'
			}
		});

		con.createBusiness(data.name + '_bar', 'layout/navbar.base', {
			'contentType': 'info',
			'autoBuild': true,
			'data': data,
			'addTitle': title
		}, function(mod, created){
			if(mod){
				mod.setData(data, title);
			}
		});


		con.createBusiness(data.name, 'advertiser/action/listInfo.main', {
			'contentType': 'info'
		}, function(mod, created){
			if(mod){
				mod.load(data.search.aderId);
			}
		});

		con.createBusiness(data.name + 'plan', 'plan/action/list.base', {
			tabText: LANG('计划'),
			tabType: 'plan',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'campaign', 'campaign/action/list.main', {
			tabText: LANG('推广组'),
			tabType: 'campaign',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'sweety', 'sweety/action/list.base', {
			tabText: LANG('创意包'),
			tabType: 'sweety',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'whisky', 'whisky/action/list.base', {
			tabText: LANG('落地页'),
			tabType: 'whisky',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'monitor', 'monitor/action/list.list', {
			tabText: LANG('广告监测'),
			tabType: 'monitor',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});
	};

	// 计划详情
	exports.onPlan = function( boot, data, app ) {
		if(utilDSP.isBrandVersion()){
			// app.controller.navigate('');
		}
		if(!app.routerValid('')) {
			return;
		}
		var title = LANG('计划详情');
		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title:title,
			type: 'tabSidebar',
			content_config: {
				'addClass': 'cool'
			}
		});

		con.createBusiness(data.name + '_bar', 'layout/navbar.base', {
			'contentType': 'info',
			'autoBuild': true,
			'data': data,
			'addTitle': title
		}, function(mod, created){
			if(mod){
				mod.setData(data, title);
			}
		});


		con.createBusiness(data.name, 'plan/action/listInfo.main', {
			'contentType': 'info'
		}, function(mod, created){
			if(mod){
				mod.load(data.search.planId);
			}
		});

		con.createBusiness(data.name + 'campaign', 'campaign/action/list.main', {
			tabText: LANG('推广组'),
			tabType: 'campaign',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'sweety', 'sweety/action/list.base', {
			tabText: LANG('创意包'),
			tabType: 'sweety',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'whisky', 'whisky/action/list.base', {
			tabText: LANG('落地页'),
			tabType: 'whisky',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});
		con.createBusiness(data.name + 'monitor', 'monitor/action/list.list', {
			tabText: LANG('广告监测'),
			tabType: 'monitor',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});
	};

	// 推广组详情
	exports.onCampaign = function( boot, data, app ) {
		if(utilDSP.isBrandVersion()){
			// app.controller.navigate('');
		}
		if(!app.routerValid('')) {
			return;
		}
		var title = LANG('推广组详情');
		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title:title,
			type: 'tabSidebar',
			content_config: {
				'addClass': 'cool'
			}
		});

		con.createBusiness(data.name + '_bar', 'layout/navbar.base', {
			'contentType': 'info',
			'autoBuild': true,
			'data': data,
			'addTitle': title
		}, function(mod, created){
			if(mod){
				mod.setData(data, title);
			}
		});

		con.createBusiness(data.name, 'campaign/action/listInfo.main', {
			'contentType': 'info'
		}, function(mod, created){
			if(mod){
				mod.load(data.search.campaignId);
			}
		});

		con.createBusiness(data.name + 'sweety', 'sweety/action/list.base', {
			tabText: LANG('创意包'),
			tabType: 'sweety',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'whisky', 'whisky/action/list.base', {
			tabText: LANG('落地页'),
			tabType: 'whisky',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});
	};

	// 广告监测详情
	exports.onMonitor = function( boot, data, app ) {
		if(utilDSP.isBrandVersion()){
			// app.controller.navigate('');
		}
		if(!app.routerValid('')) {
			return;
		}
		var title = LANG('广告监测详情');
		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title:title,
			type: 'tabSidebar',
			content_config: {
				'addClass': 'cool'
			}
		});

		con.createBusiness(data.name + '_bar', 'layout/navbar.base', {
			'contentType': 'info',
			'autoBuild': true,
			'data': data,
			'addTitle': title,
			'hasChangeUrl': true
		}, function(mod, created){
			if(mod){
				mod.setData(data, title);
			}
		});

		con.createBusiness(data.name, 'monitor/action/listInfo.main', {
			'contentType': 'info'
		}, function(mod, created){
			if(mod){
				mod.load(data.search.campaignId);
			}
		});

		con.createBusiness(data.name + 'sweety', 'sweety/action/list.base', {
			tabText: LANG('创意包'),
			tabType: 'sweety',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		con.createBusiness(data.name + 'whisky', 'whisky/action/list.base', {
			tabText: LANG('落地页'),
			tabType: 'whisky',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reload();
			}
		});

		var id = +data.search.campaignId;
		//代码获取
		con.createBusiness(data.name + 'code', 'monitor/modules/code.base', {
			tabText: LANG('代码获取'),
			tabType: 'code',
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reset();
				if(id){
					mod.load(id);
				}
			}else{
				if(id){
					mod.load(id);
				}
			}
		});


	};

	// 新建|编辑广告主
	exports.onAdvertiserEdit = function( boot, data, app ) {
		if(utilDSP.isBrandVersion()){

		}
		var id = +data.param;
		var auth = id ? 'plan.update,plan.info' : 'plan.add';

		if(!app.routerValid(auth)) {
			return;
		}

		var title = id ? LANG('编辑广告主') : LANG('新建广告主');
		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title: title,
			type: 'scroll'
		});

		// app.core.get('SYS_PLATFORM').setPlatform(title);

		con.createBusiness(data.name + '_bar', 'layout/navbar.base', {
			'contentType': 'info',
			'autoBuild': true,
			'data': data,
			'addTitle': title
		}, function(mod, created){
			if(mod){
				mod.setData(data, title);
			}
		});

		con.createBusiness(data.name, 'admin/aptitude/action/edit.main', {
			isBrand: true,
			'view_model': {
				'headTitle': LANG('')
			}
		}, function(mod) {
			mod.reset();
			if(id)
			{
				mod.setParam({_id:id}).load(id);
			}
		});
	};

	// 新建|编辑计划
	exports.onPlanEdit = function( boot, data, app ) {
		if(utilDSP.isBrandVersion()){

		}
		var id = +data.param;
		var auth = id ? 'plan.update,plan.info' : 'plan.add';

		if(!app.routerValid(auth)) {
			return;
		}

		var title = id ? LANG('编辑计划') : LANG('新建计划');
		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title: title,
			type: 'scroll'
		});

		// app.core.get('SYS_PLATFORM').setPlatform(title);

		con.createBusiness(data.name + '_bar', 'layout/navbar.base', {
			'contentType': 'info',
			'autoBuild': true,
			'data': data,
			'addTitle': title
		}, function(mod, created){
			if(mod){
				mod.setData(data, title);
			}
		});

		con.createBusiness(data.name, 'plan/action/edit.base', {
			isBrand: true,
			'layout': {
				'step':true
			}
		}, function(mod, created) {
			if(created){
				mod.reset();
			}
			if(id)
			{
				mod.load(id);
			}
		});
	};

	// 新建|编辑推广组
	exports.onCampaignEdit = function( boot, data, app ) {
		if(utilDSP.isBrandVersion()){

		}
		var id = +data.param;
		var auth = id ? 'campaign.update,campaign.info' : 'campaign.add';
		var query = data.search;
		var planId;
		if(query && query.planId)
		{
			planId = +query.planId;
		}

		if(!app.routerValid(auth))
		{
			return;
		}
		var title = id ? LANG('编辑推广组') : LANG('新建推广组');

		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title: title,
			type: 'scroll'
		});

		// app.core.get('SYS_PLATFORM').setPlatform(title);

		con.createBusiness(data.name + '_bar', 'layout/navbar.base', {
			'contentType': 'info',
			'autoBuild': true,
			'data': data,
			'addTitle': title
		}, function(mod, created){
			if(mod){
				mod.setData(data, title);
			}
		});

		con.createBusiness(data.name, 'campaign/action/edit.base', {
			isBrand: true
		}, function(mod, created) {
			if(created){
				mod.reset();
			}
			if (id){
				mod.load(id);
			}
			if(planId)
			{
				mod.setStep({'stepDom':true})
			}
		});
	};

	// 新建|编辑创意包
	exports.onSweetyEdit = function(boot,data,app){
		if(utilDSP.isBrandVersion()){

		}
		var id = data && data.param;

		var auth = id ? 'sweety.update' : 'sweety.add';
		if (!app.routerValid(auth)) {
			return;
		}

		var title = id ? LANG('编辑创意包') : LANG('新建创意包');

		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title:	title,
			type: 'scroll'
		});

		// app.core.get('SYS_PLATFORM').setPlatform(title);

		con.createBusiness(data.name + '_bar', 'layout/navbar.base', {
			'contentType': 'info',
			'autoBuild': true,
			'data': data,
			'addTitle': title
		}, function(mod, created){
			if(mod){
				mod.setData(data, title);
			}
		});

		con.createBusiness(data.name, 'sweety/action/edit.base', {
			isBrand: true
		}, function(mod, created){
			if(created){
				mod.reset();
			}
			if(id){
				mod.load(+id);
			}
		});
	};

	exports.onSweetyVerify = function(boot,data,app){

		if (!app.routerValid('&sweety.list,sweetyCreative.list')) {
			return;
		}

		var title = LANG('创意包审核详情');

		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title:title,
			type: 'tabSidebar',
			content_config: {
				'addClass': 'cool'
			}
		});

		con.createBusiness(data.name + '_bar', 'layout/navbar.base', {
			'contentType': 'info',
			'autoBuild': true,
			'data': data,
			'addTitle': title
		}, function(mod, created){
			if(mod){
				mod.setData(data, title);
			}
		});

		if(app.checkRight('sweetyCreative.list')){
			con.createBusiness(data.name + 'creativeVerify', 'sweety/action/verify.creative', {
				tabText: LANG('创意')
			}, function(mod){
			});
		}
		if(app.checkRight('sweety.list')){
			con.createBusiness(data.name + 'sweetyVerify', 'sweety/action/verify.sweety', {
				tabText: LANG('创意包')
			}, function(mod, created){
				if(created){
					mod.reload();
				}
			});
		}

	};

	// 新建|编辑落地页
	exports.onWhiskyEdit = function(boot,data,app){
		if(utilDSP.isBrandVersion()){

		}
		var id = data.param;
		var auth = id ? 'whisky.update' : 'whisky.add';
		var title = id ? LANG('编辑落地页') : LANG('新建落地页');
		if (!app.routerValid(auth)) {
			return;
		}

		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title: title,
			type: 'scroll'
		});

		// app.core.get('SYS_PLATFORM').setPlatform(title);

		con.createBusiness(data.name + '_bar', 'layout/navbar.base', {
			'contentType': 'info',
			'autoBuild': true,
			'data': data,
			'addTitle': title
		}, function(mod, created){
			if(mod){
				mod.setData(data, title);
			}
		});

		con.createBusiness(data.name, 'whisky/action/edit.base', {
			isBrand: true
		}, function(mod){
			mod.reset();

			// 编辑状态
			if(id){
				mod.load(id);
			}
		});
	};

	// 新建|编辑广告监测
	exports.onMonitorEdit = function(boot,data,app){
		if(utilDSP.isBrandVersion()){

		}

		var id = +data.param;
		var auth = id ? 'monitor.update' : 'monitor.add';
		var title = id ? LANG('编辑监测') : LANG('新建监测');
		if (!app.routerValid(auth)) {
			return;
		}

		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title: title,
			type: 'scroll'
		});

		// app.core.get('SYS_PLATFORM').setPlatform(title);

		con.createBusiness(data.name + '_bar', 'layout/navbar.base', {
			'contentType': 'info',
			'autoBuild': true,
			'data': data,
			'addTitle': title
		}, function(mod, created){
			if(mod){
				mod.setData(data, title);
			}
		});

		con.createBusiness(data.name, 'monitor/action/edit.edit', {
			isBrand: true
		}, function(mod,created){
			if(created){
				mod.reset();
			}
			if(+id){
				mod.load(id, false);
			}
		});
	};

	// 另存为广告监测
	exports.onMonitorSaveas = function( boot, data, app ) {
		if(!app.routerValid('campaign.update')) {
			return;
		}
		var id = +data.param;
		var title = LANG('监测另存为');

		if (!id) {
			app.alert(LANG('缺少监测ID'));
			return app.controller.navigate('');
		}

		var con = app.getContainer({
			scenes: 'main',
			full: false,
			sitelist: false,
			setup_btn: false,
			title: title,
			type: 'scroll'
		});

		var uri = 'monitor/action/edit.edit';

		app.core.get('SYS_PLATFORM').setPlatform(title);

		con.createBusiness(data.name, uri, {

		}, function(mod) {
			mod.load(id, true);
		});
	};
});